package service

import (
	"html/template"

	"gitee.com/Yu-eng/Myproject/my-go-blog/config"
	"gitee.com/Yu-eng/Myproject/my-go-blog/dao"
	"gitee.com/Yu-eng/Myproject/my-go-blog/models"
)

func GetAboutInfo() ([]models.Post, error) {
	posts := dao.GetAbout()
	for _, post := range posts {
		//根据分类id 拿到分类名字
		categoryName := dao.GetCategoryNameById(post.CategoryId)
		userName := dao.GetUserNameById(post.UserId)

		//对文章的内容长度进行限制，提升显示效果

		content := []rune(post.Content)
		if len(content) > 30 {
			content = content[0:30]
		}

		//将posts重新构建为postMore
		postMore := models.PostMore{
			Pid:          post.Pid,
			Title:        post.Title,
			Slug:         post.Slug,
			Content:      template.HTML(content),
			CategoryId:   post.CategoryId,
			CategoryName: categoryName,
			UserId:       post.UserId,
			UserName:     userName,
			ViewCount:    post.ViewCount,
			Type:         post.Type,
			CreateAt:     models.DateDay(post.CreateAt),
			UpdateAt:     models.DateDay(post.UpdateAt),
		}
		postMores = append(postMores, postMore)
	}

	//总页数
	//处理分页的数据
	pagecount := (total-1)/pageSize + 1

	//构建一个数组 传入pages
	var pages []int
	for i := 0; i < pagecount; i++ {
		pages = append(pages, i+1)
	}

	var hr = &models.HomeResponse{
		Viewer:    config.Cfg.Viewer,
		Categorys: categorys,
		Posts:     postMores,
		Total:     total,
		Page:      page, //当前的page
		Pages:     pages,
		PageEnd:   page != pagecount, //判断是否是最后一页，并返回
	}
	return hr, nil
}
